package com.boot.controller;

import com.boot.entity.User;
import com.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
public class UserController {

    @Autowired
    UserService userService;

    /**
     *
     * @param model
     * @param current 默认是第一页
     * @param pageSize 默认每页10条
     * @return
     */
    @GetMapping("/user/list")
    public String list(
            Model model,
            @RequestParam(value = "current",defaultValue = "1") Integer current,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        List<User> list = userService.list(current, pageSize);
        model.addAttribute("list",list);
        model.addAttribute("current",current);

        int count = userService.count();
        //计算总的页数
        model.addAttribute("pages",( count % pageSize == 0 ? count / pageSize : count / pageSize + 1));

        return "user/list";
    }

}
